<?php
if (!class_exists('BrodevAnswerInstaller')) {
    class BrodevAnswerInstaller
    {
        var $bNoInstall = false;
        function install()
        {
            if ($this->bNoInstall) {
                return false;
            }
            $oDb  = Phpfox::getLib('phpfox.database');
            $aSql = array(
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_answer') . "`",
                "CREATE TABLE `" . Phpfox::getT('brodev_answer_answer') . "` (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `user_id` int(11) NOT NULL,
                    `question_id` int(11) NOT NULL,
                    `detail` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                    `created` int(11) NOT NULL,
                    PRIMARY KEY (`id`)
                )",
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_category') . "`",
                "CREATE TABLE `" . Phpfox::getT('brodev_answer_category') . "` (
                      `id` int(11) NOT NULL AUTO_INCREMENT,
                      `name` varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                      `parent_id` int(11) NOT NULL,
                      `ordering` INT NOT NULL,
                      PRIMARY KEY (`id`)

                )",
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_point') . "`",
                "CREATE TABLE `" . Phpfox::getT('brodev_answer_point') . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `user_id` int(11) NOT NULL,
                  `action` int(11) NOT NULL,
                  `question_id` int(11) NOT NULL,
                  `answer_id` int(11) NOT NULL,
                  `up` tinyint(1) NOT NULL,
                  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                  PRIMARY KEY (`id`)

                )",
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_question') . "`",
                "CREATE TABLE `" . Phpfox::getT('brodev_answer_question') . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `user_id` int(11) NOT NULL,
                  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                  `category_id` int(11) NOT NULL,
                  `detail` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                  `tag` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                  `time_stamp` int(11) NOT NULL,
                  `view` int(11) NOT NULL,
                  `total_like` int(11) NOT NULL,
                  `total_comment` int(11) NOT NULL,
                  `is_liked` int(11) NOT NULL,
                  `featured` tinyint(1) NOT NULL,
                  `status` INT NOT NULL,
                  PRIMARY KEY (`id`)
                )",
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_level') . "`",
                "CREATE TABLE `" . Phpfox::getT('brodev_answer_level') . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `group_id` int(11) NOT NULL,
                  `maxpoint` int(11) NOT NULL,
                  `minpoint` int(11) NOT NULL,
                  `name` VARCHAR( 150 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
                  `previous_group` INT( 11 ) NOT NULL,
                  PRIMARY KEY (`id`)

                )",
                "ALTER TABLE `" . Phpfox::getT('user_activity') . "` ADD `activity_answers` INT NOT NULL "
            );
            foreach ($aSql as $sSql) {
                $oDb->query($sSql);
            }
        }
        function upgrade()
        {
        }
        function uninstall()
        {
            $oDb             = Phpfox::getLib('phpfox.database');
            $aActivityPoints = $oDb->select('activity_total, activity_points, activity_answers, user_id')->from(Phpfox::getT('user_activity'))->execute('getRows');
            foreach ($aActivityPoints as $aVal) {
                $aUpdate = array(
                    'activity_total' => $aVal['activity_total'] - $aVal['activity_answers'],
                    'activity_points' => $aVal['activity_points'] - $aVal['activity_answers']
                );
                $oDb->update(Phpfox::getT('user_activity'), $aUpdate, 'user_id = ' . $aVal['user_id']);
            }
            $aSql = array(
                'DROP TABLE IF EXISTS `' . Phpfox::getT('brodev_answer_answer') . '`',
                "DROP TABLE IF EXISTS `" . Phpfox::getT('brodev_answer_category') . "`",
                'DROP TABLE IF EXISTS `' . Phpfox::getT('brodev_answer_point') . '`',
                'DROP TABLE IF EXISTS `' . Phpfox::getT('brodev_answer_question') . '`',
                'DROP TABLE IF EXISTS `' . Phpfox::getT('brodev_answer_level') . '`',
                'DELETE FROM `' . Phpfox::getT('feed') . '` WHERE `type_id` like \'%answer%\'',
                'DELETE FROM `' . Phpfox::getT('notification') . '` WHERE `type_id` like \'%answer%\'',
                'ALTER TABLE `' . Phpfox::getT('user_activity') . '` DROP `activity_answers`'
            );
            foreach ($aSql as $sSql) {
                $oDb->query($sSql);
            }
        }
    }
}
$oRequest = Phpfox::getLib('phpfox.request');
if ($oRequest->get('upgrade')) {
    $sType = 'upgrade';
} else if ($oRequest->get('delete')) {
    $sType = 'uninstall';
} else if ($oRequest->get('install')) {
    $sType = 'install';
}
$oInstaller = new BrodevAnswerInstaller();
$oInstaller->{$sType}();